package com.zbkj.front.controller;

import com.zbkj.common.model.order.Order;
import com.zbkj.common.model.saleroom.SaleRoom;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.vo.LogisticsResultVo;
import com.zbkj.front.service.FrontOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("api/front/order")
@Api(tags = "订单")
public class OrderController {

    @Autowired
    private FrontOrderService orderService;


    @ApiOperation(value = "预下单")
    @RequestMapping(value = "/pre/order", method = RequestMethod.POST)
    public CommonResult<OrderNoResponse> preOrder(@RequestBody @Validated PreOrderRequest request) {
        return CommonResult.success(orderService.preOrder_V1_7(request));
    }


    @ApiOperation(value = "加载预下单")
    @RequestMapping(value = "load/pre/{preOrderNo}", method = RequestMethod.GET)
    public CommonResult<PreOrderResponse> loadPreOrder(@PathVariable String preOrderNo) {
        return CommonResult.success(orderService.loadPreOrder(preOrderNo));
    }


    @ApiOperation(value = "计算订单价格")
    @RequestMapping(value = "/computed/price", method = RequestMethod.POST)
    public CommonResult<ComputedOrderPriceResponse> computedPrice(@Validated @RequestBody OrderComputedPriceRequest request) {
        return CommonResult.success(orderService.computedOrderPrice(request));
    }


    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<OrderNoResponse> createOrder(@Validated @RequestBody CreateOrderRequest orderRequest) {
        return CommonResult.success(orderService.createOrder(orderRequest));
    }


    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderFrontDataResponse>> orderList(@ModelAttribute @Validated OrderFrontListRequest request) {
        return CommonResult.success(CommonPage.restPage(orderService.list_v1_4(request)));
    }


    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public CommonResult<OrderFrontDetailResponse> orderDetail(@PathVariable String orderNo) {
        return CommonResult.success(orderService.frontDetail(orderNo));
    }


    @ApiOperation(value = "根据订单号模糊查询订单")
    @RequestMapping(value = "/getOrder/{orderNo}", method = RequestMethod.GET)
    public CommonResult<List<Order>> getOrder(@PathVariable String orderNo) {
        return CommonResult.success(orderService.getOrder(orderNo));
    }


    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/cancel/{orderNo}", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.cancel(orderNo)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "拒接收货-带原因")
    @RequestMapping(value = "/orderCancel", method = RequestMethod.POST)
    public CommonResult<Boolean> orderCancel(@RequestBody @Valid OrderCancelRequest orderCancelRequest) {
        if (orderService.orderCancel(orderCancelRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "订单商品评论列表")
    @RequestMapping(value = "/reply/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<InfoReplyResponse>> replyList(@ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(CommonPage.restPage(orderService.replyList(pageRequest)));
    }


    @ApiOperation(value = "评价订单商品")
    @RequestMapping(value = "/reply/product", method = RequestMethod.POST)
    public CommonResult<Boolean> replyProduct(@RequestBody @Validated OrderProductReplyRequest request) {
        if (orderService.replyProduct(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "删除订单")
    @RequestMapping(value = "/delete/{orderNo}", method = RequestMethod.POST)
    public CommonResult<Boolean> delete(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.delete(orderNo)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "订单收货")
    @RequestMapping(value = "/take/delivery/{orderNo}", method = RequestMethod.POST)
    public CommonResult<String> take(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.takeDelivery(orderNo)) {
            return CommonResult.success("订单收货成功");
        }
        return CommonResult.failed("订单收货失败");
    }


    @ApiOperation(value = "用户确认订单(确认收货、确认签收等等)")
    @RequestMapping(value = "/order/confirm/{orderNo}", method = RequestMethod.POST)
    public CommonResult<String> confirm(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.confirm(orderNo)) {
            return CommonResult.success("订单确认成功");
        }
        return CommonResult.failed("订单确认失败");
    }


    @ApiOperation(value = "获取订单发货单列表")
    @RequestMapping(value = "/{orderNo}/invoice/list", method = RequestMethod.GET)
    public CommonResult<OrderInvoiceFrontResponse> getInvoiceList(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(orderService.getInvoiceList(orderNo));
    }


    @ApiOperation(value = "物流信息查询")
    @RequestMapping(value = "/logistics/{invoiceId}", method = RequestMethod.GET)
    public CommonResult<LogisticsResultVo> getLogisticsInfo(@PathVariable(value = "invoiceId") Integer invoiceId) {
        return CommonResult.success(orderService.getLogisticsInfo(invoiceId));
    }


    @ApiOperation(value = "获取订单状态图")
    @RequestMapping(value = "/status/image", method = RequestMethod.GET)
    public CommonResult<List<HashMap<String, Object>>> getOrderStatusImage() {
        return CommonResult.success(orderService.getOrderStatusImage());
    }

//    /**
//     * @param position    地理定位
//     * @param shopSpecIds 商品规格ids
//     * @return
//     */
//    @ApiOperation(value = "到店自提下单页面到店自提时获取门店接口")
//    @RequestMapping(value = "/recently/salerooms", method = RequestMethod.POST)
//    public CommonResult<List<SaleRoom>> getSalerooms(@RequestParam(value = "position") String position,
//                                                     @RequestParam(value = "shopSpecIds") String shopSpecIds,
//                                                     @RequestParam(value = "city", required = false) String city) {
//        return CommonResult.success(orderService.getSalerooms(position, shopSpecIds, city));
//    }


    @Deprecated
    @ApiOperation(value = "到店自提下单页面到店自提时获取门店接口")
    @RequestMapping(value = "/recently/salerooms", method = RequestMethod.POST)
    public CommonResult<List<SaleRoom>> getSaleroomsList(@RequestBody @Validated ProductAttrRequest productAttrRequest) {
        return CommonResult.success(orderService.getSaleroomsList(productAttrRequest));
    }


    @ApiOperation(value = "同城配送判断门店是否位置超过30KM/门店缺货,返回最近的一个门店")
    @RequestMapping(value = "/position/isover", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> selectRecentlyRoomId(@RequestBody @Validated ProductAttrRequest productAttrRequest) {
        return CommonResult.success(orderService.getRecentlyRoom(productAttrRequest));
    }


    @ApiOperation(value = "根据订单号查询配送员的经纬度")
    @RequestMapping(value = "/get/location", method = RequestMethod.POST)
    public CommonResult<Map> getLocation(@RequestBody @Validated OrderGetLocationRequest orderGetLocationRequest) {
        return CommonResult.success(orderService.getLocationV2(orderGetLocationRequest));
    }


}
